package suncertify.view;

import javax.swing.*;
import java.awt.*;

/**
 * Swing utilities class
 */
public class SwingUtils {
    /**
     * moves a component to screen center
     * @param com component
     */
    public static void placeCenter(Component com) {
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        com.setLocation((d.width - com.getWidth()) / 2, (d.height - com.getHeight()) / 2);
    }

    /**
     * moves a component to parent component center
     * @param parent parent component
     * @param com component
     */
    public static void placeCenter(Component parent, Component com) {
        Dimension d = parent.getSize();
        Point p = parent.getLocation();
        com.setLocation(p.x + (d.width - com.getWidth()) / 2, p.y + (d.height - com.getHeight()) / 2);
    }

    /**
     * displays error dialog with message
     * @param parent parent component
     * @param msg error message
     */
    public static void showError(Component parent, String msg) {
        JOptionPane.showMessageDialog(parent, msg, "Error", JOptionPane.OK_OPTION);
    }

    /**
     * displays confirm dialog with message
     * @param parent parent component
     * @param msg message
     * @return selected JOptionPane option constant, YES - 0, NO - 1
     */
    public static int showConfirm(Component parent, String msg) {
        return JOptionPane.showConfirmDialog(parent, msg, "warning", JOptionPane.YES_NO_OPTION);
    }
}
